﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2008 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2008-8-13 17:43:17
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using DALFactory;
using Model;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
namespace BLL
{
    /// <summary>
    /// 业务逻辑类 USERS
    /// </summary>
    public class USERS : RemoveCaches
    {
        private readonly IDAL.IUSERS dal = DataAccess.CreateUSERS();
        public USERS()
            : base("USERS_")
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.USERS model)
        {
            string password = FormsAuthentication.HashPasswordForStoringInConfigFile(model.USERS_PASSWORD, "MD5").ToString();
            model.USERS_PASSWORD = password;
            string data = dal.Add(model);
            if (Caches.EnableCache && data != null)
            {
                RemoveCountCache();
                RemoveMaxCache();
                RemoveListCache();
            }
            return data;
        }

        /// <summary>
        /// 增加多条数据
        /// </summary>
        public void Add(List<Model.USERS> l)
        {
            foreach (Model.USERS model in l)
                dal.Add(model);

            if (Caches.EnableCache)
            {
                RemoveCountCache();
                RemoveMaxCache();
                RemoveListCache();
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.USERS model)
        {
            bool b = dal.Update(model);
            if (Caches.EnableCache && b)
            {
                RemoveModelCache(model.USERS_CODE);
                RemoveListCache();
                RemoveCountCache();
            }
            return b;
        }

        /// <summary>
        /// 更新多条数据
        /// </summary>
        public void Update(List<Model.USERS> l)
        {
            foreach (Model.USERS model in l)
            {
                bool b = dal.Update(model);
                if (Caches.EnableCache && b)
                    RemoveModelCache(model.USERS_CODE);
            }
            if (Caches.EnableCache)
            {
                RemoveListCache();
                RemoveCountCache();
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string USERS_CODE)
        {
            bool b = dal.Delete(USERS_CODE);
            if (Caches.EnableCache && b)
            {
                RemoveExistsCache(USERS_CODE);
                RemoveModelCache(USERS_CODE);
                RemoveListCache();
                RemoveMaxCache();
                RemoveCountCache();
            }
            return b;
        }

        /// <summary>
        /// 删除多条数据
        /// </summary>
        public void Delete(List<string> l)
        {
            foreach (string USERS_CODE in l)
            {
                bool b = dal.Delete(USERS_CODE);
                if (Caches.EnableCache && b)
                {
                    RemoveExistsCache(USERS_CODE);
                    RemoveModelCache(USERS_CODE);
                }
            }
            if (Caches.EnableCache)
            {
                RemoveListCache();
                RemoveMaxCache();
                RemoveCountCache();
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string USERS_CODE)
        {
            if (!Caches.EnableCache)
                return dal.Exists(USERS_CODE);

            string key = basicKey + "Exists_" + USERS_CODE.ToString();
            if (HttpRuntime.Cache[key] != null)
                return (bool)HttpRuntime.Cache[key];
            else
            {
                bool data = dal.Exists(USERS_CODE);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetUSERSCacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.USERS GetModel(string USERS_CODE)
        {
            if (!Caches.EnableCache)
                return dal.GetModel(USERS_CODE);

            string key = basicKey + "Model_" + USERS_CODE.ToString();
            if (HttpRuntime.Cache[key] != null)
                return (Model.USERS)HttpRuntime.Cache[key];
            else
            {
                Model.USERS data = dal.GetModel(USERS_CODE);
                //AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetUSERSCacheDependency();
                //Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), System.Web.Caching.CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 得到数据总条数
        /// </summary>
        public int GetCount()
        {
            if (!Caches.EnableCache)
            {
                DataSet ds = dal.GetDataSet("count(*)", null, null);
                return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }

            string key = basicKey + "Counts_All";
            if (HttpRuntime.Cache[key] != null)
                return (int)HttpRuntime.Cache[key];
            else
            {
                DataSet ds = dal.GetDataSet("count(*)", null, null);
                int data = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetUSERSCacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 获得泛型数据列表,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public List<Model.USERS> GetList(string strWhat, string strWhere, string strOrderby)
        {
            if (!Caches.EnableCache)
                return dal.GetList(strWhat, strWhere, strOrderby) as List<Model.USERS>;

            string key = basicKey + "List_" + strWhat + strWhere + strOrderby;
            if (HttpRuntime.Cache[key] != null)
                return (List<Model.USERS>)HttpRuntime.Cache[key];
            else
            {
                List<Model.USERS> data = dal.GetList(strWhat, strWhere, strOrderby) as List<Model.USERS>;
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetUSERSCacheDependency();
                Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return data;
            }
        }

        /// <summary>
        /// 分页获取泛型数据列表,不建议直接使用此方法,请根据业务逻辑重写
        /// </summary>
        public List<Model.USERS> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            if (!Caches.EnableCache)
                return dal.GetList(pageSize, pageIndex, fldSort, sort, strCondition, out pageCount, out count) as List<Model.USERS>;

            string key = basicKey + "List_" + fldSort + "_" + sort.ToString() + "_" + strCondition + "_" + pageSize.ToString() + "_" + pageIndex.ToString();
            if (HttpRuntime.Cache[key] != null)
            {
                Model.PageData data = (Model.PageData)HttpRuntime.Cache[key];
                pageCount = data.PageCount;
                count = data.Count;
                return (List<Model.USERS>)data.Data;
            }
            else
            {
                List<Model.USERS> lst = dal.GetList(pageSize, pageIndex, fldSort, sort, strCondition, out pageCount, out count) as List<Model.USERS>;
                Model.PageData data = new Model.PageData(lst, count, pageCount);
                AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetUSERSCacheDependency();
                if (pageIndex <= 3)
                    Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                else
                    Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                return lst;
            }
        }
        #endregion

        #region  扩展方法
        /// <summary>
        /// 获得全部泛型数据列表
        /// </summary>
        public List<Model.USERS> GetAllList()
        {
            return GetList(null, null, null);
        }
        #endregion








        /// <summary>
        /// 判断登录权限
        /// </summary>
        public DataRow CheckLogin(string USERS_CODE,string pwd)
        {
            string password = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5").ToString();
            DataRow data = dal.CheckLogin(USERS_CODE, password);
            return data;
        }

        /// <summary>
        /// 获得权限列表及操作数据
        /// </summary>
        public DataSet GetModuleList(string usercode, string flag)
        {
            //if (!Caches.EnableCache)
                return dal.GetModuleList(usercode, flag) as DataSet;

            //string key = basicKey + "List_ModuleList" + usercode;
            //if (HttpRuntime.Cache[key] != null)
            //    return (DataSet)HttpRuntime.Cache[key];
            //else
            //{
            //    DataSet data = dal.GetModuleList(usercode, flag) as DataSet;
            //    AggregateCacheDependency cd = CacheDependencyFactory.DependencyFacade.GetGetModuleListCacheDependency();
            //    Caches.TryAddCache(key, data, cd, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
            //    return data;
            //}
        }

        /// <summary>
        /// 系统 功能/操作 List
        /// </summary>
        public DataSet P_System_Functions_Operate_List()
        {
            return dal.P_System_Functions_Operate_List();
        }

        /// <summary>
        /// 用户组权限设置
        /// </summary>
        public bool P_UserGroup_Function_Operate_Update(string ugcode, string funccode, string operatecode, int tmp)
        {
            return dal.P_UserGroup_Function_Operate_Update(ugcode, funccode, operatecode, tmp);
        }

        /// <summary>
        /// 获取 角色组成员
        /// </summary>
        public DataSet P_User_GetGroupUserByCode(string ugcode)
        {
            return dal.P_User_GetGroupUserByCode(ugcode);
        }

        public bool UpdateUserPassword(string userId, string newPassword)
        {
            return new SQLServerDAL.USERS().UpdateUserPassword(userId, newPassword);
        }

        /// <summary>
        /// 功能：返回与传入操作员在同一个组的操作员
        /// </summary>
        /// <param name="admin">操作员ID</param>
        /// <param name="iStop">是否启用标志-1为全部,0有效的，1无效的</param>
        /// <returns></returns>
        public DataSet getSameGroupManager(string admin, int iStop)
        {
            return new SQLServerDAL.USERS().getSameGroupManager(admin, iStop);
        }
    }
}
